package cn.edu.hnu.gpg.api;

import cn.edu.hnu.gpg.api.error.NotFoundException;
import cn.edu.hnu.gpg.api.error.PermissionDeniedException;
import cn.edu.hnu.gpg.api.error.UnknownException;
import cn.edu.hnu.gpg.entity.Bulletin;
import cn.edu.hnu.gpg.entity.User;
import cn.edu.hnu.gpg.entity.enums.UserType;
import cn.edu.hnu.gpg.service.BulletinService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/api/bulletin")
public class BulletinApiController {

    private static final Logger logger = LogManager.getLogger(BulletinApiController.class);

    @Autowired
    private BulletinService bulletinService;

    @RequestMapping(value = "/publish_bulletin", method = RequestMethod.POST)
    @ResponseBody
    public int publishBulletin(HttpServletRequest request, @RequestParam String title, @RequestParam String content) throws UnknownException, PermissionDeniedException {
        User user = (User) request.getSession(false).getAttribute("user");

        if (user.getUserType() != UserType.A)
            throw new PermissionDeniedException();

        int bulletinId;

        try {
            bulletinId = bulletinService.createNewBulletin(title, content, user);
        } catch (Exception e) {
            logger.debug(e.getCause());
            throw new UnknownException();
        }

        return bulletinId;
    }

    @RequestMapping(value = "/list_bulletin", method = RequestMethod.GET)
    @ResponseBody
    public List<Bulletin> listBulletin() {
        return bulletinService.listBulletin();
    }

    @RequestMapping(value = "/{bulletinId}", method = RequestMethod.GET)
    @ResponseBody
    public Bulletin requestBulletin(@PathVariable Integer bulletinId) throws NotFoundException, UnknownException {
        try {
            Bulletin bulletin = bulletinService.queryBulletinById(bulletinId);
            if (bulletin == null)
                throw new NotFoundException("Requested bulletin is not found");

            return bulletin;

        } catch (Exception e) {
            logger.debug(e.getCause());
            throw new UnknownException();
        }
    }

    @RequestMapping(value = "/{bulletinId}", method = RequestMethod.DELETE)
    @ResponseBody
    public void deleteBulletin(HttpServletRequest request, @PathVariable Integer bulletinId) throws NotFoundException, UnknownException, PermissionDeniedException {
        User user = (User) request.getSession(false).getAttribute("user");
        if (user.getUserType() != UserType.A)
            throw new PermissionDeniedException();

        try {
            if (!bulletinService.deleteBulletin(bulletinId)) {
                throw new NotFoundException("Specified bulletin not found");
            }
        } catch (RuntimeException e) {
            logger.debug(e.getCause());
            throw new UnknownException();
        }
    }
}
